package cn.ld.module.ai.service.knowledge;

import cn.hutool.core.util.ObjUtil;
import cn.ld.framework.common.enums.CommonStatusEnum;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.common.util.object.BeanUtils;
import cn.ld.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeCreateInVO;
import cn.ld.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgePageInVO;
import cn.ld.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeUpdateInVO;
import cn.ld.module.ai.dal.entity.knowledge.AiKnowledgeEntity;
import cn.ld.module.ai.dal.entity.model.AiChatModelEntity;
import cn.ld.module.ai.dal.mapper.knowledge.AiKnowledgeMapper;
import cn.ld.module.ai.service.model.AiApiKeyService;
import cn.ld.module.ai.service.model.AiChatModelService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

import static cn.ld.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ld.module.ai.enums.ErrorCodeConstants.KNOWLEDGE_NOT_EXISTS;

/**
 * AI 知识库-基础信息 Service 实现类
 *
 * @author xiaoxin
 */
@Service
@Slf4j
public class AiKnowledgeServiceImpl implements AiKnowledgeService {

    @Resource
    private AiKnowledgeMapper knowledgeMapper;

    @Resource
    private AiChatModelService chatModelService;
    @Resource
    private AiApiKeyService apiKeyService;

    @Override
    public Long createKnowledge(AiKnowledgeCreateInVO createInVO, Long userId) {
        // 1. 校验模型配置
        AiChatModelEntity model = chatModelService.validateChatModel(createInVO.getModelId());

        // 2. 插入知识库
        AiKnowledgeEntity knowledgeBase = BeanUtils.toBean(createInVO, AiKnowledgeEntity.class)
                .setModel(model.getModel()).setUserId(userId).setStatus(CommonStatusEnum.ENABLE.getStatus());
        knowledgeMapper.insert(knowledgeBase);
        return knowledgeBase.getId();
    }

    @Override
    public void updateKnowledge(AiKnowledgeUpdateInVO updateInVO, Long userId) {
        // 1.1 校验知识库存在
        AiKnowledgeEntity knowledgeBaseEntity = validateKnowledgeExists(updateInVO.getId());
        if (ObjUtil.notEqual(knowledgeBaseEntity.getUserId(), userId)) {
            throw exception(KNOWLEDGE_NOT_EXISTS);
        }
        // 1.2 校验模型配置
        AiChatModelEntity model = chatModelService.validateChatModel(updateInVO.getModelId());

        // 2. 更新知识库
        AiKnowledgeEntity updateEntity = BeanUtils.toBean(updateInVO, AiKnowledgeEntity.class);
        updateEntity.setModel(model.getModel());
        knowledgeMapper.updateById(updateEntity);
    }

    @Override
    public AiKnowledgeEntity validateKnowledgeExists(Long id) {
        AiKnowledgeEntity knowledgeBase = knowledgeMapper.selectById(id);
        if (knowledgeBase == null) {
            throw exception(KNOWLEDGE_NOT_EXISTS);
        }
        return knowledgeBase;
    }

    @Override
    public PageResult<AiKnowledgeEntity> getKnowledgePage(Long userId, AiKnowledgePageInVO pageInVO) {
        return knowledgeMapper.selectPage(userId, pageInVO);
    }

    @Override
    public VectorStore getVectorStoreById(Long id) {
        AiKnowledgeEntity knowledge = validateKnowledgeExists(id);
        AiChatModelEntity model = chatModelService.validateChatModel(knowledge.getModelId());
        // 创建或获取 VectorStore 对象
        return apiKeyService.getOrCreateVectorStore(model.getKeyId());
    }

}
